<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
    <el-col :span="19">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="5">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>
          {{ nodeData.manager }}
        </el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!nodeData.root">
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: "TreeTools",
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    },
  },
  methods: {
    handleCommand (type) {
      // 部门删除功能
      if (type === 'del') {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return reqDelDepartments(this.nodeData.id)
        }).then((ret) => {
          if (ret.code === 10000) {
            this.$emit('on-success')
            this.$message.success('删除部门成功')
          } else if (ret.code === 99999) {
            this.$message.warning(ret.message)
          }
        }).catch((err) => {
          if (err !== 'cancel') {
            this.$message.warning('删除部门失败')
          } else if (err === 'cancel') {
            this.$message.warning('已取消删除')
          }
        })
      } else if (type === "add") { // 点击添加 弹窗
        this.$emit('toggle-box', {
          isShow: true,
          type: 'add',
          id: this.nodeData.id
        })
      } else if (type === 'edit') { // 编辑功能
        this.$emit('toggle-box', {
          isShow: true,
          type: 'edit',
          id: this.nodeData.id
        })
      }
    }
  }
}
</script>
